﻿#include <cstdlib> // For std::putenv
#include <cstring> // For std::strdup
#include <tchar.h>
#include <qwidget.h>
#include <QHBoxLayout>
#include <QIcon>
#include <QVBoxLayout>
#include "windows.h"
#include "QWindow.h"
#include <tlhelp32.h>
#include <QProcess>
#include <QBuffer>
#include <QApplication>
#include <QClipboard>
#include <QImage>
#include <QMimeData>

#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <com/sun/star/uno/Exception.hpp>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/document/IndexedPropertyValues.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XTextRange.hpp>
#include <com/sun/star/text/XTextViewCursorSupplier.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/text/XTextCursor.hpp>
#include <com/sun/star/drawing/XShape.hpp>
#include <com/sun/star/drawing/XDrawPage.hpp>
#include <com/sun/star/drawing/XDrawPages.hpp>
#include <com/sun/star/graphic/XGraphicProvider.hpp>
#include <com/sun/star/drawing/XShapes.hpp>
#include <com/sun/star/graphic/XGraphic.hpp>

#include <rtl/ustring.hxx>
#include <iostream>
#include <osl/file.hxx>

#include "MainWindow.h"
#include "BaseModule/GlobalHelper.h"
#include "insertScreenshotIntoReport.h"
#include "GenerateReportBase.h"
#include "AutomaticallySaveReport.h"

using namespace std;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::frame;
using namespace com::sun::star::text;
using namespace com::sun::star::drawing;
using namespace com::sun::star::awt;
using namespace com::sun::star::graphic;
using namespace com::sun::star::container;
using namespace com::sun::star::io;
using namespace cppu;
using namespace rtl;

using com::sun::star::beans::PropertyValue;
using com::sun::star::text::XTextDocument;
using com::sun::star::text::XText;
using ::com::sun::star::uno::Sequence;
using com::sun::star::text::XTextCursor;

namespace GenerateReport
{
	insertScreenshotIntoReport::insertScreenshotIntoReport()
	{
	}
	//test1::~Test1()
	//{
	//}

	void insertScreenshotIntoReport::actionSlot_insertScreenshotIntoReport()
	{
		getscreenshot();
		OUString imagePath = OUString("file:///") + OUString::fromUtf8(m_QImagePath.toUtf8().constData());
		auto _xTextDoc = GenerateReportBaseInstance->getTextDocument();
		auto _xContext = GenerateReportBaseInstance->getXContext();
		//auto _xComponent = GenerateReportBaseInstance->getXComponent();

		// 通过XTextViewCursorSupplier获取光标对象
		Reference<XTextViewCursorSupplier> xTextViewCursorSupplier(_xTextDoc->getCurrentController(), UNO_QUERY);
		if (!xTextViewCursorSupplier.is())
		{
			std::cerr << "Failed to get text view cursor supplier" << std::endl;
			return;
		}
		//获取XTextCursor类型的光标对象
		Reference<XTextCursor> xTextCursor;
		xTextCursor = Reference<XTextCursor>(xTextViewCursorSupplier->getViewCursor(), UNO_QUERY);

		// 显式转换为 XTextRange
		Reference<XTextRange> xTextRange(xTextCursor, UNO_QUERY);

		// 获取文档的文本对象
		Reference<XMultiServiceFactory> xDocFactory(_xTextDoc, UNO_QUERY_THROW);
		//创建图片
		Reference<XShape> xShape = Reference<XShape>(xDocFactory->createInstance("com.sun.star.drawing.GraphicObjectShape"), UNO_QUERY);
		//设置图片路径
		Reference<XGraphicProvider> xGraphicProvider(_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.graphic.GraphicProvider", _xContext), UNO_QUERY);
		Sequence<PropertyValue> aMediaProps(1);
		aMediaProps[0].Name = "URL";
		aMediaProps[0].Value <<= imagePath;
		Reference<XGraphic> xGraphic = xGraphicProvider->queryGraphic(aMediaProps);

		// 获取形状对象的属性集
		Reference<XPropertySet> xPropSet(xShape, UNO_QUERY);
		// 将XGraphic对象设置到形状对象的属性中
		xPropSet->setPropertyValue("Graphic", makeAny(xGraphic));
		//设置图片的大小
		Size aNewSize;
		aNewSize.Width = m_width * 15;//长宽根据实际设置(默认单位是：缇(Twips))
		aNewSize.Height = m_height * 15;
		xShape->setSize(aNewSize);

		Reference<XTextContent> xTextContent(xShape, UNO_QUERY);
		if (!xTextContent.is())
		{
			std::cerr << "xTextContent wrong! " << std::endl;
			return;
		}

		Reference <XText> xText = _xTextDoc->getText();
		if (!xText.is())
		{
			std::cerr << "xText wrong! " << std::endl;
			return;
		}
		xText->insertTextContent(xTextRange, xTextContent, true);
	}

	void insertScreenshotIntoReport::getscreenshot()
	{
		// 获取剪切板对象
		QClipboard* clipboard = QApplication::clipboard();
		// 获取剪切板中的图像
		QImage image = clipboard->image();
		if (!image.isNull())
		{
			//std::cout << "image width: " << image.width() << "; height: " << image.height() << std::endl;
			m_width = image.width();
			m_height = image.height();
			// 保存到本地供Libreoffice读取
			//m_QImagePath = QString("F:/LibreofficeTest/tempQImageForLibreOffice.jpg");
			m_QImagePath = GlobalHelperInstance->getProjectTempIODir() + "/tempQImageForLibreOffice.jpg";
			image.save(m_QImagePath);
		}
		else
		{
			std::cout << "no image" << std::endl;
		}
	}

}
